package kr.co.sdsway2.service;

import java.util.HashMap;

import javax.annotation.Resource;

import kr.co.sdsway2.common.AbstractService;
import kr.co.sdsway2.dao.PollInfDAO;
import kr.co.sdsway2.dao.PtInfDAO;
import kr.co.sdsway2.vo.PollInfVO;
import kr.co.sdsway2.vo.PtInfVO;

import org.springframework.stereotype.Service;

@Service
public class PollQstService extends AbstractService{

	@Resource(name="ptInfDAO")
	PtInfDAO ptInfDAO;

	@Resource(name="pollInfDAO")
	PollInfDAO pollInfDAO;

	
//	@Resource(name="pollObjOptInfDAO")
//	PollObjOptInfDAO pollObjOptInfDAO;
	
	//d
//	@Resource(name="pollObjQstDAO")
//	PollObjQstDAO pollObjQstDAO;

	//d
//	@Resource(name="pollSubQstDAO")
//	PollSubQstDAO pollSubQstDAO;

//	@Resource(name="pollQstMap")
//	HashMap<String, Object> pollQstMap;
	@Resource(name="ptInfBeanService")
	PtInfBeanService ptInfBeanService;
	
	@Resource(name="pollInfBeanService")
	PollInfBeanService pollInfBeanService;

	/** 설문 시작  */
	public HashMap<String, Integer> insertPollQst(HashMap<String, String> inputMap) {
		
//		Integer ptId = ptInfDAO.getPtId();
//		if( ptId == null ) {
//			error("PT_ID 생성 실패");
//			return null;
//		}

		//임시
		final Integer PT_ID  = 1;

		String qstTypeCd = inputMap.get("qstTypeCd");
		String qstVal = inputMap.get("qstVal");
		String opt1 = inputMap.get("objOpt1");
		String opt2 = inputMap.get("objOpt2");
		String opt3 = inputMap.get("objOpt3");
		String opt4 = inputMap.get("objOpt4");
		Integer optPoint1,optPoint2,optPoint3,optPoint4;
		try{
			optPoint1 = Integer.parseInt(inputMap.get("optPoint1"));
		}catch(NumberFormatException e){
			optPoint1 = 0;
		}
		try{
			optPoint2 = Integer.parseInt(inputMap.get("optPoint2"));
		}catch(NumberFormatException e){
			optPoint2 = 0;
		}
		try{
			optPoint3 = Integer.parseInt(inputMap.get("optPoint3"));
		}catch(NumberFormatException e){
			optPoint3 = 0;
		}
		try{
			optPoint4 = Integer.parseInt(inputMap.get("optPoint4"));
		}catch(NumberFormatException e){
			optPoint4 = 0;
		}
		
		//1. INSERT INTO POLL_INF S ==============================
		Integer pollId = pollInfDAO.getPollId();
		if( pollId == null ) {
			error("POLL_ID 생성 실패");
			return null;
		}
		PollInfVO pollInfVO = new PollInfVO();
		pollInfVO.setPtId(PT_ID);
		pollInfVO.setPollId(pollId);
		pollInfVO.setQstTypeCd(qstTypeCd);
		pollInfVO.setQstVal(qstVal);	//문제
		pollInfVO.setOpt1(opt1);	//보기1
		pollInfVO.setOpt2(opt2);	//보기2
		pollInfVO.setOpt3(opt3);	//보기3
		pollInfVO.setOpt4(opt4);	//보기4
		pollInfVO.setOptPoint1(optPoint1);	//배점1
		pollInfVO.setOptPoint2(optPoint2);	//배점2
		pollInfVO.setOptPoint3(optPoint3);	//배점3
		pollInfVO.setOptPoint4(optPoint4);	//배점4
		pollInfDAO.insertPollInf(pollInfVO);
		//1. INSERT INTO POLL_INF E ==============================

		//2. INSERT OR UPDATE INTO PT_INF S ==============================
		PtInfVO ptInfVO = new PtInfVO();
		ptInfVO.setPtId(PT_ID);
		ptInfVO.setPtStatCd("30");		//30: 설문진행
		ptInfVO.setPstPollId(pollId);	//현재 진행 설문 ID 입력
		ptInfDAO.updatePtInf(ptInfVO);
		//2. INSERT OR UPDATE INTO PT_INF E ==============================

		//3. bean 설정 S ==============================
		ptInfBeanService.update(PT_ID, "30", pollId);
		pollInfBeanService.update(PT_ID,pollId, qstTypeCd, qstVal,opt1,opt2,opt3,opt4,optPoint1,optPoint2,optPoint3,optPoint4);
		//3. bean 설정 E ==============================
		
		//4. return value 설정 S ==============================
		HashMap<String, Integer> outputMap = new HashMap<String, Integer>();
		outputMap.put("ptId", PT_ID);
		outputMap.put("pollId", pollId);
		//4. return value 설정 E ==============================
		
		return outputMap;
	}

	/** 설문 종료 */
	public void deletePollQst(String ptId,String pollId) {
		//1. PT 정보 테이블 상태변경 S ==============================
		PtInfVO vo = new PtInfVO();
		vo.setPtId(Integer.parseInt(ptId));
		vo.setPtStatCd("40");
		vo.setPstPollId(null);
		ptInfDAO.updatePtInf(vo);
		ptInfBeanService.update(Integer.parseInt(ptId), "40", null);
		//1. PT 정보 테이블 상태변경 E ==============================
//		pollQstMap.clear();
	}

	/** 설문 조회 */
	public HashMap<String, Object> getPollQst() {
		//return pollQstMap;
		return null;
	}

}
